package com.zhu.wechat.controller;

import com.zhu.wechat.Dao.RoomRepository;
import com.zhu.wechat.entity.Room;
import com.zhu.wechat.service.SendWxCpMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SendWxCpMsgController {

    @Autowired
    private SendWxCpMsgService sendWxCpMsgService;

    @Autowired
    private RoomRepository roomRepository;

    @GetMapping("/send-wx-cp-msg")
    public String sendWxCpMsg(@RequestParam String username) {
        sendWxCpMsgService.sendToWxCp(username);
        return "Message sent successfully to " + username + ". 前往企业微信查看 <a href='https://work.weixin.qq.com/wework_admin/frame#apps'>点击这里</a>";
    }

    @PostMapping("/send-cleaning-request")
    public String sendCleaningRequest(@RequestBody CleaningRequest request) {
        List<String> roomNumbers = request.getRoomNumber();
        for (String roomNumber : roomNumbers) {
            // 根据房号查询用户信息
            Room room = roomRepository.findByRoomNumber(roomNumber);
            if (room != null) {
                String username = room.getUsername();
                sendWxCpMsgService.sendToWxCp(username);
                return "打扫请求已发送 ";
            }
        }
        return "未找到";
    }
/**
 * 查找所有房号
 *
 * @return java.util.List<com.zhu.wechat.entity.Room>
 * @author zhuwanyi
 * @create 2024/12/24
 **/

@GetMapping("/get-room-numbers")
    public List<Room> getRoomNumbers() {
        return roomRepository.findAll();
    }

    public static class CleaningRequest {
        private List<String> roomNumbers;

        public List<String> getRoomNumber() {
            return roomNumbers;
        }

        public void setRoomNumbers(List<String> roomNumbers) {
            this.roomNumbers = roomNumbers;
        }


    }
}
